package com.ffms.servlet; /**
 * @author 从卿
 * @create 2022/3/13-16:39
 */

import com.ffms.entity.Admin;
import com.ffms.entity.FamilyMember;
import com.ffms.service.AdminService;
import com.ffms.service.impl.AdminServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/AddAdminServlet")
public class AddAdminServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.接收视图层的请求数据
        String userName = request.getParameter("username");
        String password = request.getParameter("password");
        int i = 0;
        AdminService adminService = new AdminServiceImpl();
        Admin admin = new Admin(userName, password);
        i = adminService.register(admin);


        //根据结果跳转到不同的页面 成功：主界面 失败：原界面
        if (i > 0) {
            //成功跳转到主页面
            request.setAttribute("success1", "注册成功！");
            request.getRequestDispatcher("/login.jsp").forward(request, response);

        } else {

            request.setAttribute("error", "用户名已存在");
            request.getRequestDispatcher("/admin/adminAdd.jsp").forward(request, response);
        }
    }
}


